<template>
  <div class="login-container">
    <div class="login-card">
      <div class="logo-box">
        <img alt="" class="logo" src="@/assets/img/login-logo.png" />
        <span class="logo-name">Neta Simulator</span>
      </div>
        <el-form ref="loginForm" :model="loginForm" :rules="rules" class="login-form">
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="请输入账号"
              prefix-icon="el-icon-user"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              placeholder="请输入密码"
              prefix-icon="el-icon-lock"
              show-password
              @keyup.enter.native="handleLogin"
            ></el-input>
          </el-form-item>
          <el-button
            type="primary"
            class="loginBtn"
            @click="handleLogin"
          >
            登录
          </el-button>
          <p class="tip">
            <router-link :to="{name:'Register'}"> 还没有账号？点击注册</router-link>
          </p>
        </el-form>
      </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在3到15个字符', trigger: 'blur' }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    }
  },
  methods: {
     handleLogin() {
      try {
          this.$refs.loginForm.validate((valid) => {
            if (valid) { 
            this.$store.dispatch('user/_login', {username:this.loginForm.username.trim(),password:this.loginForm.password}).then(res => {
              if(res.code === 0){
                this.$message({
                  message: '用户登陆成功',
                  type: 'success',
                  center: true
                })
                this.$store.commit('app/SET_SHOW_TOKEN_MESSAGE', false)
                const redirect = this.$route.query.redirect || '/'; // 获取之前页面的路径，默认为根路径
                this.$router.push(redirect); // 重定向到之前页面
              }else{
                this.$message({
                  message: res.msg,
                  type: 'error',
                  center: true
                })
              }   
            }).catch( (err)=> {
              this.$message({
                message: '用户登陆失败',
                type: 'error',
                center: true
              })
            })
          }
        }) 
      }
      catch (err) {
        throw err
      }
    
    }
  }
}
</script>

<style lang="scss" scoped>
@import '@/styles/vars.scss';
.login-container {
  width: 100vw;
  height: 100vh;
  box-shadow: 5px 4px 100px 74px #ffffff inset;
  background: url("~@/assets/img/login-bg.png");
  background-attachment: fixed;
  background-size: cover;
  background-repeat: no-repeat;
  .login-card {
    position: absolute;
    width: 386px;
    height: 480px;
    right: 8%;
    padding: 10px;
    top: calc(50% - 300px);
    background: #fff;
    border-radius: 10px;
    border: 1px solid rgba(0, 0, 0, 0.12);
    .logo-box {
      margin: 40px auto 40px;
      margin-left: 36px;
    }

    .logo {
      height: 48px;
      margin-right: 5px;
      vertical-align: middle;
    }

    .logo-name {
      position: relative;
      top: 8px;
      color: rgb(38 144 237);
      font-weight: 600;
      font-size: 26px;
    }
    .login-form{
      text-align: center;
      .el-form-item{
        margin-bottom: 32px;
      }
      ::v-deep .el-input{
        .el-input__inner{
          width: 318px;
          height: 42px;
        }
        .el-input__prefix{
          left: 24px;
        }
        .el-input__suffix{
          right: 24px;
        } 
      }
      ::v-deep .el-form-item__error{
          left: 24px !important;
        }
      .loginBtn{
        width: 318px;
      }
    }
    
  }
  .tip{
    margin-top: 10px;
    float: right;
    margin-right: 24px;
    font-size: 13px;
    a:hover{
      color: $primary-color !important;
    }
  }
}
</style>
